﻿




	using System.Collections.Generic;
    using System;
	using System.Linq;    
    using System.Data;        
	using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
using DeklaritModel;

namespace WebSilverlight.Web
{
	[EnableClientAccess()]    
    public partial class ClienteDomainService : DomainService
    { 
		#region Properties						
		protected DeklaritModel.ClienteDataSet ContextDataSet;
		// Setear Property (global o anivel de entidad o ambas?)
		const int PageSize = 6;
        
        public virtual IClienteDataAdapter DataAdapter 
        {
            get 
            {
				if(DataAdapterFactory.Provider == null) //return null;
					DataAdapterFactory.Provider = new SimpleDataAdapterFactory();

                return DataAdapterFactory.GetClienteDataAdapter(); 
            }
        }
		
		#endregion
        
        private void FillClienteDataSet(DeklaritModel.ClienteDataSet dsClienteDataSet1, int page, Int16? ClienteId , Int16? fkPaisId)
        {   
            if (ClienteId.HasValue )
            {   
				DataAdapter.Fill(dsClienteDataSet1, ClienteId.Value);                            
            }
            else
            {
			    if(fkPaisId.HasValue)
				{
					if (page == -1)
						DataAdapter.FillByPaisId(dsClienteDataSet1, fkPaisId.Value);
                    else DataAdapter.FillPageByPaisId(dsClienteDataSet1, fkPaisId.Value, (page - 1) * PageSize, PageSize);
				}
				else				
					if (page == -1)
						DataAdapter.Fill(dsClienteDataSet1);
					else DataAdapter.FillPage(dsClienteDataSet1, (page-1) * PageSize, PageSize);				
            }      
        } 
        private IQueryable<ClienteWrapper> GetPage(int pageNumber, Int16? fkPaisId)
        {
            ContextDataSet = new DeklaritModel.ClienteDataSet();
            FillClienteDataSet(ContextDataSet, pageNumber, null , fkPaisId);

            EnumerableRowCollection<DataRow> dr = ContextDataSet.Cliente.AsEnumerable();
            
			IEnumerable<ClienteWrapper> query = from row in            
                            dr
                        select new ClienteWrapper
                        {
							ClienteId = row.Field<Int16>("ClienteId"),
							ClienteName = row.Field<string>("ClienteName"),
							ClienteGender = row.Field<Int16>("ClienteGender"),
							ClienteAge = row.Field<Int16>("ClienteAge"),
							ClienteBirthday = row.Field<DateTime>("ClienteBirthday"),
							ClienteIsActive = row.Field<bool>("ClienteIsActive"),
							PaisId = row.Field<Int16>("PaisId"),
							PaisNombre = row.Field<string>("PaisNombre"),
							ClientePicture = row.Field<Byte[]>("ClientePicture"),
                        };            
            IQueryable<ClienteWrapper> iq = query.AsQueryable();            
            return iq;            
        } 
                       
        public IQueryable<ClienteWrapper> GetClientePage(int pageNumber)
        {
            return GetPage(pageNumber , null);
        }       
		public IQueryable<ClienteWrapper> GetCliente()
        {
            return GetPage(-1 , null);
        }
        public IQueryable<ClienteWrapper> GetClienteByPaisId(Int16 PaisId)
        {            
            return GetPage(-1, PaisId);
        }  
        private void GetCliente(Int16? ClienteId)
        {
			ContextDataSet = new DeklaritModel.ClienteDataSet();
            FillClienteDataSet(ContextDataSet, -1, ClienteId.Value, null);
        }
		public void UpdateCliente(ClienteWrapper current)        
        {			
			GetCliente(current.ClienteId);

            DataRow updateRow = null;
            foreach (DataRow row in ContextDataSet.Cliente.Rows)
            {
                updateRow = row;                
                break;
            }

            //var Changes = this.ChangeSet.GetOriginal(current);
			//if (Changes.ClienteId != current.ClienteId)
                updateRow.SetField("ClienteId", current.ClienteId);							
			//if (Changes.ClienteName != current.ClienteName)
                updateRow.SetField("ClienteName", current.ClienteName);							
			//if (Changes.ClienteGender != current.ClienteGender)
                updateRow.SetField("ClienteGender", current.ClienteGender);							
			//if (Changes.ClienteAge != current.ClienteAge)
                updateRow.SetField("ClienteAge", current.ClienteAge);							
			//if (Changes.ClienteBirthday != current.ClienteBirthday)
                updateRow.SetField("ClienteBirthday", current.ClienteBirthday);							
			//if (Changes.ClienteIsActive != current.ClienteIsActive)
                updateRow.SetField("ClienteIsActive", current.ClienteIsActive);							
			//if (Changes.PaisId != current.PaisId)
                updateRow.SetField("PaisId", current.PaisId);							
			//if (Changes.PaisNombre != current.PaisNombre)
                updateRow.SetField("PaisNombre", current.PaisNombre);							
			//if (Changes.ClientePicture != current.ClientePicture)
                updateRow.SetField("ClientePicture", current.ClientePicture);							
			DataAdapter.Update(ContextDataSet);    
        }        
        public void InsertCliente(ClienteWrapper newObj)        
        {			
			DeklaritModel.ClienteDataSet  dsClienteDataSet1 = new DeklaritModel.ClienteDataSet();            
			ClienteDataSet.ClienteRow row = dsClienteDataSet1.Cliente.NewClienteRow();
			row.ClienteId  = newObj.ClienteId;
			row.ClienteName  = newObj.ClienteName;
			row.SetField("ClienteGender", newObj.ClienteGender);	
			//row.ClienteGender  = newObj.ClienteGender;
			row.ClienteAge  = newObj.ClienteAge;
			row.ClienteBirthday  = newObj.ClienteBirthday;
			row.ClienteIsActive  = newObj.ClienteIsActive;
			row.PaisId  = newObj.PaisId;
			row.PaisNombre  = newObj.PaisNombre;
			row.ClientePicture  = newObj.ClientePicture;
            dsClienteDataSet1.Cliente.AddClienteRow(row);
            DataAdapter.Update(dsClienteDataSet1);             
        }
        public void DeleteCliente(ClienteWrapper delObj)        
        {			
			DeklaritModel.ClienteDataSet  dsClienteDataSet1 = new DeklaritModel.ClienteDataSet();            
			ClienteDataSet.ClienteRow row = dsClienteDataSet1.Cliente.NewClienteRow();
			row.ClienteId  = delObj.ClienteId;
			row.ClienteName  = delObj.ClienteName;
			row.SetField("ClienteGender", delObj.ClienteGender);				
			row.ClienteAge  = delObj.ClienteAge;
			row.ClienteBirthday  = delObj.ClienteBirthday;
			row.ClienteIsActive  = delObj.ClienteIsActive;
			row.PaisId  = delObj.PaisId;
			row.PaisNombre  = delObj.PaisNombre;
			row.ClientePicture  = delObj.ClientePicture;
            dsClienteDataSet1.Cliente.RemoveClienteRow(row);
            DataAdapter.Update(dsClienteDataSet1);             
        }
        [Invoke]
        public int ItemCount()
        {            
            return DataAdapter.GetRecordCount();
        }
	}
}